const sequelize = require('../../../core/sequlize_pool').sequelize;

const path = require('path');
const read = require('fs-readdir-recursive');
const basename = path.basename(module.filename);
const db = {};

read(__dirname)
    .filter(function(file) {
        return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
    }).forEach(function(file) {
        var model = sequelize["import"](path.join(__dirname, file));
        db[model.name] = model;
    });

Object.keys(db).forEach(function(modelName) {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});

module.exports = db;